import 'dart:math' as math;
import 'dart:math';

class FFMath {
  //角度转狐度
  static double deg2rad(double deg) {
    return deg * (math.pi / 180);
  }

  ///狐度转角度
  static double rad2deg(double rad) {
    return rad * (180 / math.pi);
  }

  ///
  /// 匹配范围
  ///
  static double matchRange(double value, {
    double valueLeft = 0,
    double valueRight = 1,
    double tagetLeft = 0,
    double tagetRight = 1,
  }) {
    // 如果原始范围的最大值和最小值相等，避免除以0
    if (valueLeft == valueRight) {
      throw ArgumentError("The min and max values cannot be the same");
    }

    if (valueRight < valueLeft) {
      double tmp = valueRight;
      valueRight = valueLeft;
      valueLeft = tmp;
    }

    // 计算比例
    double scale = (value - valueLeft) / (valueRight - valueLeft);
    var result = tagetLeft + scale * (tagetRight - tagetLeft);
    return result;
  }

  static double matchRange2({
    required double value,
    required double valueLeft,
    required double valueRight,
    required double tagetLeft,
    required double tagetRight,
  }) {
    // 如果原始范围的最大值和最小值相等，避免除以0
    if (valueLeft == valueRight) {
      throw ArgumentError("The min and max values cannot be the same");
    }

    if (valueRight < valueLeft) {
      double tmp = valueRight;
      valueRight = valueLeft;
      valueLeft = tmp;
    }

    // 计算比例
    double scale = (value - valueLeft) / (valueRight - valueLeft);
    var result = tagetLeft + scale * (tagetRight - tagetLeft);
    return result;
  }

  /**
   * @title 值在两者之间
   */
  static double clamp(double value, double min, double max) {
    assert(min <= max, "FFMath clamp min <= max");
    return min + (max - min) * value;
  }

  /**
   * @title 值在两者之间，并且严格限制
   */
  static double clampMinMax(double value, double min, double max) {
    assert(min <= max, "FFMath clamp min <= max");
    if (value < min) {
      return min;
    }

    if (value > max) {
      return max;
    }

    return value;
  }
}
